import { createContext, useContext } from 'react';

import { globalConfig } from '@/config';

type LangContextType = {
  locale: App.I18n.LangType;
  localeOptions: App.I18n.LangOption[];
  setLocale: (locale: App.I18n.LangType) => void;
};

const LangContext = createContext<LangContextType>({
  locale: globalConfig.defaultLang,
  localeOptions: globalConfig.defaultLangOptions,
  setLocale: globalConfig.noop,
});

const useLang = () => {
  const context = useContext(LangContext);
  if (!context) {
    throw new Error('useLang must be used within a LangProvider');
  }
  return context;
};

export { type LangContextType, LangContext, useLang };
